package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.adapter.CycleListAdapter;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.presenter.ICycleSelectPresenter;
import com.dchh.android.suverynew.presenter.impl.CycleSelectPresenter;
import com.dchh.android.suverynew.view.ICycleSelectView;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Period;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;
import com.dhcc.android.base.utils.StringUtil;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 调查期选择
 *
 * @author zhoudan
 * @date 2015年5月19日
 */
public class CycleSelectActivity extends SBaseActivity implements ICycleSelectView {

    public static final String MJDC_CODE = "002,003,004,007";
    public static final String CLDC_CODE = "005,006,008";
    /**
     * 显示列表的ListView
     */
    private ListView listview;
    /**
     * 列表适配器
     */
    private CycleListAdapter adapter;
    /**
     * 存放调查周期内容的数据集合
     */
    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    /**
     * raw中的调查周期集合
     */
    private List<Period> plist;
    private String root;

    private String taskName;

    private ICycleSelectPresenter mCycleSelectPresenter = new CycleSelectPresenter(this);
    private InputStream is;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_cycle_select);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("调查期选择");
    }

    /**
     * 初始化list
     */
    @Override
    public void initView() {
        taskName = getIntent().getStringExtra("taskName");
        listview = (ListView) findViewById(R.id.cycle_listview);
        // 1.解析config.xml
        is = this.getResources().openRawResource(R.raw.menu);
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View parent,
                                    int position, long arg3) {
                if (StringUtil.isFastDoubleClick(500)) {
                    return;
                }
                //判断是否是对应的调查期
                String pid = list.get(position).get("id");
                String pname = list.get(position).get("name");
                if (!pid.equals("000") && !pid.equals("001") && !pid.equals("005")) { //不能是 摸底调查 和 种植意向
                    // 跳转到下一个页面
                    Period p = new Period();
                    p.setCode(pid);
                    p.setLabel(pname);
                    Constants.currentCycle = p;

                    Intent intent = new Intent();
                    intent.setClass(CycleSelectActivity.this, TableSelectActivity.class);
                    intent.putExtra("periodCode", pid);
                    intent.putExtra("periodLabel", pname);
                    intent.putExtra("taskName", taskName);
                    startActivity(intent);

                } else {
                    CommenUtil.toast("该调查期 未开放！", CycleSelectActivity.this);
                }

            }
        });
        mCycleSelectPresenter.initData(is);
        // 恢复异常关闭
        mCycleSelectPresenter.restore();
    }


    /**
     * 刷新（重新获取）
     */
    private void refresh() {
        this.mCycleSelectPresenter.initData(is);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onRestart() {
        if (Constants.data_refresh) {
            Constants.data_refresh = false;
            refresh();
        }
        super.onRestart();
    }

    @Override
    public void onInitDataReturn(List<Map<String, String>> list) {
        this.list = list;
        if (adapter == null) {
            adapter = new CycleListAdapter(this, list);
        }
        listview.setAdapter(adapter);
    }

    @Override
    public void onRestoreData(Period p) {
        Intent intent = new Intent();
        intent.setClass(CycleSelectActivity.this, TableSelectActivity.class);
        intent.putExtra("periodCode", p.getCode());
        intent.putExtra("periodLabel", p.getLabel());
        intent.putExtra("taskName", taskName);
        startActivity(intent);
    }
}
